<?php
/* $Id: mysql.class.php 672 2008-08-09 01:15:41Z enrico $ */

require_once "lib/BaseLibrary.class.php";
require_once "lib/text/TextProcessor.class.php";


class MySQL extends BaseLibrary {

	private $mysql_config_class	= null;

	private $currentConnection 	= null;

	private $queryObject_callback_list	= null;

	/**
	 * Konstruktor
         **
         * @param $mysql_config Typ Klasse configurations/mysql.config.class.php
	 */
	
	function MySQL( $mysql_config_class ) {
		
		/* Superklasse aufrufen */
		$this->BaseLibrary( get_class($this), array( "mysql", "mysqli") );

		/* Umgebungs Abfrage */
		$this->base_environment_extensions_neededAtLeastOrDie_html( 
			1,
			"Ich benoetige mindestens eine der Erweiterungen {baselibrary_extension_listAsString}"
		);
		
		$this->mysql_config_class = $mysql_config_class;

		//echo $this->mysql_config_class->getHostname();

	}

	/**
	 * Erstellt eine neue Verbindung mit den Werten aus der Config 
         **
	 * @return bool	true=Erfolgreiche Verbindung; false=fehlgeschlagene Verbindung
	 */

	function connect() {

		$this->currentConnection = mysql_connect( 
			$this->mysql_config_class->getHostname(),	/* Server */
			$this->mysql_config_class->getLoginUsername(),	/* Benutzername */
			$this->mysql_config_class->getLoginPassword(),	/* Password */
			$this->mysql_config_class->getIsAlwaysNewConnection(), 	/* Neue Verbindung um jeden Preis, seit PHP 4.2.0 */
			$this->mysql_config_class->getServerClientFlags() 	/* Server-Client Flags, seit PHP 4.3.0 */
		);

		/* Es konnte keine Verbindung hergestellt werden */
		if ( !$this->currentConnection) {
			$this->currentConnection = null;
			return false;
		};


		/* Verbindung erfolgreich */
		return true;

	}	

	function setCurrentConnection( $currentConnection ) {
		$this->currentConnection = $currentConnection;
	}

	function getCurrentConnection() {
		return $this->currentConnection;
	}

	function query_db( $database, $query, $workerMethod_call ) {


	}

	/**
	 * Aehnlich obiges query_db, benutzt aber die in der Config gespeicherte Datenbank
	 */

	function query( $query ) {

		if ($this->currentConnection != null) {
			
			//echo "Query! (".$query.")";
			return mysql_query( $query, $this->currentConnection);


		} else
			return null;

	}

	/**
         * Registriert eine Callback Funktion unter dem Namen $nameTrigger
         **
         * @param $method callback in der Form array(<Class>, <Method>)
	 * @param $nameTrigger Name
	 */

	function registerObjectMethod( $nameTrigger, $method ) {
		$this->queryObject_callback_list[$nameTrigger] = $method;
	}

	function getQueryObject( $query, $errormsg ) {
		$q = $this->query( $query );
		
		//return ($q === false) ? false: mysql_fetch_object( $q );
		//return (mysql_fetch_object($q));

		if ( $q !== false ) {
			return mysql_fetch_object($q);

		} /*else
			$this->setErrorStatus($errormsg, true );
*/
		return false;

	}

	function processRow( $queryCommand, $callback_trigger ) {

		$query = $this->query($queryCommand);
		if (  $query != null ) {

			//echo "Array";

			$lastResult = null;
			while ( $query_object = mysql_fetch_object($query) ) {
				$callback = $this->queryObject_callback_list[$callback_trigger];
				$lastResult = call_user_func($callback, $query_object);
			}

			return $lastResult;
		} else
			return false;

	}

	function processUniqueRow( $queryCommand, $callback_trigger ) {

		$query = $this->query($queryCommand);
		if (  $query != null ) {

			$query_object = mysql_fetch_object($query);
			$callback = $this->queryObject_callback_list[$callback_trigger];

			return call_user_func( $callback, $query_object );

		} else
			return false;

	}

	function processRowToConcatedElements( $queryCommand, $callback_trigger ) {

		$query = $this->query($queryCommand);
		if (  $query != null ) {

			//echo "Array";
			$lastResult = null;
			while ( $query_object = mysql_fetch_object($query) ) {
				$callback = $this->queryObject_callback_list[$callback_trigger];
				//echo "Callback: ".$callback;
				//print_r($callback);
				$lastResult .= call_user_func($callback, $query_object);
			}

			return $lastResult;
		} else
			return false;

	}


	/**
	 * nl2br XHTML Konform ab v 4.0.5
	 */
	function getFlattenedTrustedHTMLValueHashArray_SQL_key_value ( $object_arr ) {
		
		$s = ""; $i = 0;
		foreach( $object_arr as $key=>$value ) {
			$s .= ( ($i==0) ? "": ",")." `".$key.
				"` = '".
					addslashes( TextProcessor::getNiceXHTMLFromString($value) ).
				"'";
			$i++;
		}

		//echo $s;
		return $s;

	} 

	function getFlattenedKeysOnly( $object_arr ) {

		$s = ""; $i = 0;
		foreach( $object_arr as $key=>$value ) {
			$s .= ( ($i==0) ? "": ",")." `".$key.
				"`"; 
			$i++;
		}

		//echo $s;
		return $s;

	}

	function getSQLConfig_class() {
		return $this->mysql_config_class;

	}

	function getTablenameByObject( $table_delimiter, $obj ) {

		foreach( $obj as $key=>$value ) {


		}

	}

	/** SQL Grundfunktionen **/

	function sql_change_table_row_by_id( $table, $triggerId, $id, $changedvalues_arr ) {

		/* Flatten Objekt */

		$stripped_string = $this->getFlattenedTrustedHTMLValueHashArray_SQL_key_value( 
					$changedvalues_arr
		);


		$q = 	"UPDATE ".
				$this->getSQLConfig_class()->getDatabaseName().".".
				$this->getSQLConfig_class()->getTablesPrefix().
				$table.
			" SET ".$stripped_string.
			" WHERE `".$triggerId."`=".$id;

		return $this->query( $q );

		
	}



}



?>
